﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BankingSystem.BLL;
using Model;

namespace BankClas
{
    public partial class Form1 : Form
    {
        private IBank bankServis;
        private Client _curentClient;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bankServis = new Bank();
        }

        private void button1_Click(object sender, EventArgs e)
        { 
            ClientInfo clientinfo = new ClientInfo();
            clientinfo.FistName = Convert.ToString(FistName.Text);
            clientinfo.LastName = Convert.ToString(LastName.Text);
            clientinfo.PassportNumber = Convert.ToString(Passport.Text);
            bankServis.CreateClient(clientinfo);
            MessageBox.Show("Клиент добавлен");
            FistName.Clear();
            LastName.Clear();
            Passport.Clear();

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (NomPassport.Text != "")
            {
                string s = Convert.ToString(NomPassport.Text);
                var client = bankServis.FindByCriteria(x => x.Information.PassportNumber == s).FirstOrDefault();

                if (client == null)
                {
                    MessageBox.Show("Not found!");
                }

                else
                {

                    if (radioButton1.Checked)
                    {
                        richTextBox1.Text = string.Format("клиент {0}", client.Information.FistName);
                    }
                    else if (radioButton2.Checked)
                    {
                        Old_Name.Text = client.Information.FistName;
                        Old_Sur.Text = client.Information.LastName;
                        Old_Pass.Text = client.Information.PassportNumber;
                    }
                    else
                    {
                        MessageBox.Show("Выберите режим просмотра!");
                    }

                    _curentClient = client;
                }
            }

            else
            {
                MessageBox.Show("Введите номер паспорта для поиска!");
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string client12 = Convert.ToString(textBox14.Text);
            var client = bankServis.FindByCriteria(x => x.Information.PassportNumber == client12).FirstOrDefault();

            if (client == null)
            {
                MessageBox.Show("Not found!");
            }
            else
            {
                richTextBox1.Text = string.Format("клиент удален {0}", client.Information.FistName);
                bankServis.DeleteClient(client);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(NewName.Text))
            {
                _curentClient.Information.FistName = NewName.Text;
            }
            
            if (!String.IsNullOrEmpty(NewSur.Text))
            {
                _curentClient.Information.LastName = NewSur.Text;
            }
            if (!String.IsNullOrEmpty(NewPass.Text))
            {
                _curentClient.Information.PassportNumber = NewPass.Text;
            }
            bankServis.UpdateClient(_curentClient);
            MessageBox.Show("Client Update ");
            NewName.Clear();
            NewSur.Clear();
            NewPass.Clear();

            Old_Name.Text = _curentClient.Information.FistName;
            Old_Sur.Text = _curentClient.Information.LastName;
            Old_Pass.Text = _curentClient.Information.PassportNumber;

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (_curentClient != null)
            {
                bankServis.CreateAccount(_curentClient, DateTime.Now, Double.Parse(Balans.Text));
                MessageBox.Show("Счет создан");

            }
            else
            {
                MessageBox.Show("Няма клиента");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            var clientAccounts = bankServis.FindClientAccounts(_curentClient);
            dataGridView1.DataSource = clientAccounts;
            accountFirst.Text = clientAccounts.First().Id;

        }

        private void NomPassport_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (accountButton_1.Checked)
            {
                Account firstAccount = bankServis.FindAccountByID(accountFirst.Text).First();
                bankServis.Deposite(firstAccount, Double.Parse(accountMoney.Text));
            }
            else if (accountButton_2.Checked)
            {
                Account firstAccount = bankServis.FindAccountByID(accountFirst.Text).First();
                bankServis.WithDraw(firstAccount, Double.Parse(accountMoney.Text));
            }
            else if (accountButton_4.Checked)
            {
                Account firstAccount = bankServis.FindAccountByID(accountFirst.Text).First();
                bankServis.DeleteAccount(firstAccount);
            }
            MessageBox.Show("Операция выполнена успешно");
        }

       
    }
}
